const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

const publicRequest =(url,data={},methods='GET')=>{
  return new Promise((resolve,reject)=>{
    wx.request({
      url: url,
      data:data,
      methods:methods,
      header: {
        cookie: wx.getStorageSync('cookies')?wx.getStorageSync('cookies').find(item => item.indexOf('MUSIC_U') !== -1):''
      },
      success:(res)=> {
        if(data.isLogin){
          wx.setStorage({
            data: res.cookies,
            key: 'cookies',
          })
        }
        console.log("请求成功！",res)
        resolve(res.data)
      },
      fail:(err)=>{
        console.log("请求失败",err)
        reject(err)
      }
    })
  })
}
module.exports = {
  formatTime,
  publicRequest
}
